import java.util.*;
import java.io.*;

public class MyBot1 {
    // The DoTurn function is where your code goes. The PlanetWars object
    // contains the state of the game, including information about all planets
    // and fleets that currently exist. Inside this function, you issue orders
    // using the pw.IssueOrder() function. For example, to send 10 ships from
    // planet 3 to planet 8, you would say pw.IssueOrder(3, 8, 10).
    //
    // There is already a basic strategy in place here. You can use it as a
    // starting point, or you can throw it out entirely and replace it with
    // your own. Check out the tutorials and articles on the contest website at
    // http://www.ai-contest.com/resources.
    public static void DoTurn(PlanetWars pw) throws Exception {
        boolean logging = true;
        PrintWriter out = null;
        long startTime = System.nanoTime();
        long lastCycleStart = System.nanoTime();
        long avg = 0;
        ArrayList<Long> cycleTimes = new ArrayList<Long>();
        System.out.println("#blahaioekrhnionrfdiohn");
        if (logging)
        {
            out = new PrintWriter(new FileWriter(".\\outputfile.txt", true));

            out.println("\n\n******\n\n");
            out.println("New turn:");
            out.println("Start time: " + (System.nanoTime()));

        }

        do
        {
            // (1) If we currently have a fleet in flight, just do nothing.
            if (logging)
            {
                out.println("Fleets in the air: " + pw.MyFleets().size());
            }
//            out.println("Max fleets: " + ((pw.MyPlanets().size() / 2) + 1));
//            if (pw.MyFleets().size() >= (pw.MyPlanets().size() / 2) + 1) {
//                out.close();
//                return;
//            }
            // (2) Find my strongest planet.
            Planet source = null;
            double sourceScore = Double.MIN_VALUE;
            for (Planet p : pw.MyPlanets()) {
                double score = (double)p.NumShips();
                if (score > sourceScore) {
                    sourceScore = score;
                    source = p;
                }
            }
            // (3) Find the weakest enemy or neutral planet.
            Planet dest = null;
            double destScore = Double.MIN_VALUE;
            ArrayList<Planet> fleetsTo = new ArrayList<Planet>();
            boolean fleetOnTheWay = false;
            for (Planet p : pw.NotMyPlanets()) {
                for (Fleet f : pw.MyFleets())
                {
                    if (f.DestinationPlanet() == p.PlanetID())
                    {
                        fleetOnTheWay = true;
                        fleetsTo.add(p);
                    }
                }
                double score = 1.0 / (1 + p.NumShips());
                if (score > destScore && !fleetOnTheWay) {
                    destScore = score;
                    dest = p;
                }
                fleetOnTheWay = false;
            }
            // (4) Send half the ships from my strongest planet to the weakest
            // planet that I do not own.
            if (source != null && dest != null) {
                int numShips = source.NumShips() / 2;
                pw.IssueOrder(source, dest, numShips);
            }
            long cycleLength = System.nanoTime() - lastCycleStart;
            cycleTimes.add(cycleLength);
            avg = 0;
            for(Long cLength:cycleTimes)
            {
                avg += cLength;
            }
            avg = (long)((double)avg/(double)(cycleTimes.size()));
            lastCycleStart = System.nanoTime();
            if (logging)
            {
                out.println("Average cycle time: " + avg);
                out.println("Time elapsed this turn: " + (lastCycleStart - startTime));
                out.println("Predicted runtime: " + (((lastCycleStart - startTime) + avg)));
            }
        }
        //while(((lastCycleStart - startTime) + avg) < 500000);
        while(false);
        if (logging)
        {
            out.close();
        }
    }

    public static void main(String[] args) {
	String line = "";
	String message = "";
	int c;
	try {
	    while ((c = System.in.read()) >= 0) {
		switch (c) {
		case '\n':
		    if (line.equals("go")) {
			PlanetWars pw = new PlanetWars(message);
			DoTurn(pw);
		        pw.FinishTurn();
			message = "";
		    } else {
			message += line + "\n";
		    }
		    line = "";
		    break;
		default:
		    line += (char)c;
		    break;
		}
	    }
	} catch (Exception e) {
	    // Owned.
	}
    }
}

